package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.data.ResCommentListData;
import com.cskaoyan.service.CommentService;
import com.cskaoyan.service.CommentServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/admin/comment/*")
public class AdminCommentServlet extends WdBaseServlet {
    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        ResCommentListData commentListData = new ResCommentListData();
        //接收参数
        if(request.getParameter("userId")!=null&&!"".equals(request.getParameter("userId"))){
            commentListData.setUserId(Integer.parseInt(request.getParameter("userId")));
        }
        if(request.getParameter("limit")!=null&&!"".equals(request.getParameter("limit"))){
            commentListData.setLimit(Integer.parseInt(request.getParameter("limit")));
        }
        if(request.getParameter("valueId")!=null&&!"".equals(request.getParameter("valueId"))){
            commentListData.setValueId(Integer.parseInt(request.getParameter("valueId")));
        }
        if(request.getParameter("page")!=null&&!"".equals(request.getParameter("page"))){
            commentListData.setPage(Integer.parseInt(request.getParameter("page")));
        }
        if(request.getParameter("order")!=null){
            commentListData.setOrder(request.getParameter("order"));
        }
        if(request.getParameter("sort")!=null){
            commentListData.setSort(request.getParameter("sort"));
        }

        CommentService commentService = new CommentServiceImpl();
        CommonData<MarketComment> commonData = commentService.list(commentListData);
        return BaseRespVo.ok(commonData);
    }
    @SneakyThrows
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response){
        CommentService commentService = new CommentServiceImpl();
        ResCommentListData commentListData = new ResCommentListData();
        String jsonStr = request.getReader().readLine();
        MarketComment comment = JacksonUtil.read(jsonStr, MarketComment.class);
        commentService.delete(comment);

        return BaseRespVo.ok();
    }

}
